﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Demos.util;
using mendian;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.面料库存.库存记录.service;
using DXApplicationTangche.UC.面料库存.入库.service;
using Kingdee.BOS.WebApi.Client;
using static DXApplicationTangche.UC.金蝶.dto.MaterialResultDTO;
using DXApplicationTangche.UC.金蝶.dto;
using static DXApplicationTangche.UC.金蝶.dto.MaterialDTO;
using Newtonsoft.Json.Linq;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.Utils.Win;
using DevExpress.XtraLayout;
using DXApplicationTangche.UC.面料库存.入库;
using DXApplicationTangche.UC.面料审核.Service;

namespace DXApplicationTangche.UC.面料库存
{
    public partial class Frm新增面料期初 : DevExpress.XtraEditors.XtraForm
    {
        public String fnumber { get; set; }
        public String fshortname { get; set; }
        public String fname { get; set; }
        public String fabricComposition { get; set; }
        private FabricCompositionDTO fabricCompositionDTO = new FabricCompositionDTO();
        public Frm新增面料期初()
        {
            InitializeComponent();
            //this.searchLookUpEdit供应商.Properties.DataSource = 入库Service.getSupplierAll();
        }

        private void simpleButton提交_Click(object sender, EventArgs e)
        {
            ////判断面料号输入是否合法、面料号是否重复
            //MaterialMessageResult messageResult =
            //    入库Service.面料号合法性判断(this.textEdit面料号.Text, this.textEdit描述名称.Text, this.textEdit成分.Text);
            //if (messageResult.isLeagel)
            //{
            if (String.IsNullOrWhiteSpace(this.textEdit面料号.Text.Trim()) 
                || String.IsNullOrWhiteSpace(this.textEdit描述名称.Text.Trim())
                || String.IsNullOrWhiteSpace(this.textEdit供货商.Text.Trim())
                || String.IsNullOrWhiteSpace(this.textEdit成分.Text.Trim())
                )
            {
                MessageBox.Show("面料号、面料描述、面料成分、供应商为必填");
                return;
            }
                DTO面料库存 DTO面料库存 = new DTO面料库存(
                    this.textEdit供货商.Text.Trim()
                    , this.textEdit面料号.Text.Trim()
                    , this.textEdit描述名称.Text
                    , this.textEdit成分.Text
                    , this.textEdit纱织密度.Text
                    , this.textEdit克重.Text
                    , this.textEdit幅宽.Text
                    , this.textEdit厚度.Text
                    , this.textEdit弹力.Text
                    , this.textEdit面料特性注意.Text
                    , this.textEdit延续性.Text
                    , this.textEdit用途.Text
                    , this.textEdit验料结果.Text
                    , this.textEdit颜色.Text
                    , this.textEdit季节.Text
                    , this.textEdit风格.Text
                    , this.textEdit整理方式.Text
                    );
                //转圈控件
                this.splashScreenManager.ShowWaitForm();
                this.splashScreenManager.SetWaitFormCaption("请稍后,正在加载中...."); // 标题
                this.splashScreenManager.SetWaitFormDescription("正在初始化....."); // 信息
                //金蝶保存物料-提交、审核
                //KindDeeApplyReturnDTO kindDeeApplyReturnDTO = 入库Service.金蝶物料保存(this.fnumber, DTO面料库存);
                //if (kindDeeApplyReturnDTO.IsSuccess)
                //{
                FunctionHelper.JsonSerialization(DTO面料库存);
                ////调用保存面料接口
                RestCall.PostHttp(URL.接口url + "/material/saveMaterialWithKingDeeInfo?fId=" + "" + "&fNumber=" + "",
                    FunctionHelper.JsonSerialization(DTO面料库存)
                    );
                //webservice传值
                入库Service.post新增面料(DTO面料库存);
                    //创建完成后关闭转圈
                    this.splashScreenManager.CloseWaitForm();
                    MessageBox.Show("面料创建成功");
                //}
                //else
                //{
                //    this.splashScreenManager.CloseWaitForm();
                //    MessageBox.Show("金蝶保存物料失败，面料创建失败");
                //}
                this.Close();
            //}
            //else
            //{
            //    MessageBox.Show(messageResult.errorMessage);
            //    //this.textBox查找面料号.Clear();
            //    //this.textEdit面料号.Text = "";
            //}
        }

        #region
        private void searchLookUpEdit供应商_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear供应商Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit供应商View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit供应商.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.fnumber = this.searchLookUpEdit供应商.Properties.View.GetRowCellValue(rowHandle, "fnumber").ToString();
                    this.fshortname = this.searchLookUpEdit供应商.Properties.View.GetRowCellValue(rowHandle, "fshortname").ToString();
                    this.fname = this.searchLookUpEdit供应商.Properties.View.GetRowCellValue(rowHandle, "fname").ToString();
                }
                this.textEdit供货商.Text = this.fshortname;
                this.textBox供应商名称.Text = this.fname;
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear供应商Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit供应商.ToolTip = null;
            searchLookUpEdit供应商.EditValue = null;
        }
        private void searchLookUpEdit供应商_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
            {
                e.DisplayText = this.fshortname;
            }
        }
        #endregion
        #region
        private void searchLookUpEdit成份_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear成份Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit成份View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit供应商.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    //this.fnumber = this.searchLookUpEdit成份.Properties.View.GetRowCellValue(rowHandle, "fnumber").ToString();
                    //this.fshortname = this.searchLookUpEdit成份.Properties.View.GetRowCellValue(rowHandle, "fshortname").ToString();
                    //this.fname = this.searchLookUpEdit成份.Properties.View.GetRowCellValue(rowHandle, "fname").ToString();
                }
                this.textEdit供货商.Text = this.fshortname;
                this.textBox供应商名称.Text = this.fname;
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear成份Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit供应商.ToolTip = null;
            searchLookUpEdit供应商.EditValue = null;
        }
        private void searchLookUpEdit成份_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
            {
                e.DisplayText = this.fshortname;
            }
        }
        #endregion

        private void textBox查找面料号_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                //判断面料号输入框是否为空
                if (!String.IsNullOrWhiteSpace(this.textBox查找面料号.Text.Trim()))
                {
                    //判断面料号是否存在
                    //当面料号已存在
                    if (入库Service.fabricExisted(this.textBox查找面料号.Text.Trim()))
                    {
                        MessageBox.Show("面料号已存在，请勿重复创建");
                        this.textBox查找面料号.Clear();
                    }
                }
                else
                {
                    MessageBox.Show("请输入面料号");
                }
            }
        }
        private void 清空控件()
        {
            this.textBox查找面料号.Clear();
            this.textBox供应商名称.Clear();
            this.searchLookUpEdit供应商.Text = "";
            this.textEdit供货商.Text = "";
            this.textEdit面料号.Text = "";
            this.textEdit描述名称.Text = "";
            this.textEdit成分.Text = "";
            this.textEdit纱织密度.Text = "";
            this.textEdit克重.Text = "";
            this.textEdit幅宽.Text = "";
            this.textEdit厚度.Text = "";
            this.textEdit弹力.Text = "";
            this.textEdit面料特性注意.Text = "";
            this.textEdit延续性.Text = "";
            this.textEdit用途.Text = "";
            this.textEdit验料结果.Text = "";
            this.textEdit颜色.Text = "";
            this.textEdit季节.Text = "";
            this.textEdit风格.Text = "";
            this.textEdit整理方式.Text = "";
        }

        private void simpleButton清空_Click(object sender, EventArgs e)
        {
            this.清空控件();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            new Form保存面料成份(this.fabricCompositionDTO).ShowDialog();
            this.textEdit成分.Text = this.fabricCompositionDTO.fabricComposition;
        }
    }
}